linecount++;
memset(&buff, '\0', sizeof(buff));
fgets(buff, sizeof(buff), file_in);
-
+
if (strlen(buff)) {
wpt_tmp = xcalloc(sizeof(*wpt_tmp), 1);
-
s = buff;
+
/* data delimited by commas, not enclosed */
s = csv_lineparse(s, ",", "", linecount);
wpt_tmp->position.longitude.degrees = atof(s);
break;
case 2:
- wpt_tmp->description = xstrdup(s);
- wpt_tmp->description = csv_stringtrim(wpt_tmp->description, " ");
+ wpt_tmp->description = csv_stringtrim(s, " ");
break;
default:
fprintf (stderr, "%s: Warning: unmapped data fields on line %d.\n",
}
wpt_tmp->creation_time = time(NULL);
+
+ /* We'll make up our own shortname. */
+ wpt_tmp->shortname = mkshort(wpt_tmp->description);
+
waypt_add(wpt_tmp);
} else {
}
static void
-gpsutil_disp(waypoint *wpt)
+csv_waypt_pr(const waypoint *wpt)
{
double lon,lat;
+ char * description = NULL;
lon = wpt->position.longitude.degrees;
lat = wpt->position.latitude.degrees;
if (wpt->description)
- wpt->description = csv_stringclean(wpt->description, ",\"");
+ description = csv_stringclean(wpt->description, ",\"");
fprintf(file_out, "%08.5f, %08.5f, %s\n",
lat,
lon,
- wpt->description);
+ description);
+
+ if (description)
+ free (description);
}
static void
data_write(void)
{
- waypt_disp_all(gpsutil_disp);
+ waypt_disp_all(csv_waypt_pr);
}
-
ff_vecs_t csv_vecs = {
rd_init,
wr_init,
/*********************************************************************/
/* csv_stringclean() - remove any unwanted characters from string. */
-/* returns MODIFIED string. */
-/* usage: csv_stringclean(stringtoclean, "&,\"") */
+/* returns copy of string. */
+/* usage: p = csv_stringclean(stringtoclean, "&,\"") */
/* (strip out ampersands, commas, and quotes. */
/*********************************************************************/
char *
-csv_stringclean(char *string, const char *chararray) {
- char * p;
- char * lp;
+csv_stringclean(const char *string, const char *chararray) {
+ char * p1;
+ char * p2;
const char * cp;
+ char * tmp = xstrdup(string);
if ((! string) || (! chararray)) {
- return (string);
+ return (tmp);
}
-
- /* lp - end of the original string */
- lp = string;
- while (*lp) lp++;
+
+ /* p2 - end of the original string */
+ p2 = tmp;
+ while (*p2) p2++;
cp = chararray;
while (*cp) {
- p = string;
- while (*p) {
- if (*cp == *p) {
+ p1 = tmp;
+ while (*p1) {
+ if (*cp == *p1) {
/* we don't want this character! */
- strncpy(p, p+1, (lp - p));
+ strncpy(p1, p1 + 1, (p2 - p1));
+ p1[p2 - p1] = '\0';
}
- p++;
+ p1++;
}
cp++;
}
- return (string);
+ return (tmp);
}
/***********************************************************************************/
/* csv_stringtrim() - trim whitespace and leading and trailing enclosures (quotes) */
-/* returns MODIFIED string. */
-/* usage: csv_stringtrim(string, "\"") */
+/* returns a copy of the modified string */
+/* usage: p = csv_stringtrim(string, "\"") */
/***********************************************************************************/
char *
-csv_stringtrim(char *string, const char *enclosure)
+csv_stringtrim(const char *string, const char *enclosure)
{
- static char *p1 = NULL;
+ static const char *p1 = NULL;
char *p2 = NULL;
+ char * tmp = xstrdup(string);
size_t elen;
- if (!string) {
- return (string);
+ if (!strlen(string)) {
+ return (tmp);
}
if (!enclosure) {
elen = strlen(enclosure);
}
- p2 = string;
+ p2 = tmp;
+ p1 = tmp;
- /* advance pointer to the end of the string */
- while ((*p2) && (p2++)) {
- }
+ /* advance p2 to the end of the string, then back it off. */
+ while ((*p2) && (p2++)) { }
p2--;
/* trim off trailing whitespace */
while (isspace(*p2)) {
- *p2 = '\0';
p2--;
}
- p1 = string;
-
/* advance p1 past any leading whitespace */
while (isspace(*p1)) {
p1++;
}
- /* if we have enclosures, yank them out in pairs */
+ /* if we have enclosures, skip past them in pairs */
if (elen) {
- while ((strncmp(p1, enclosure, elen) == 0)
+ while ((strncmp(tmp, enclosure, elen) == 0)
&& (strncmp(p2, enclosure, elen) == 0)) {
- *p2 = '\0';
- p2--;
- p1++;
+ p2 -= elen;
+ p1 += elen;
}
}
- return (p1);
+ /* copy what's left over back into tmp. */
+ strncpy(tmp, p1, (p2 - p1) + 1);
+ tmp[(p2 - p1) + 1] = '\0';
+
+ return (tmp);
}
/*****************************************************************************/
/* csv_lineparse() - extract data fields from a delimited string. designed */
/* to handle quoted and delimited data within quotes. */
/* returns temporary COPY of delimited data field (use it */
-/* or lose it). */
+/* or lose it on the next call). */
/* usage: p = csv_lineparse(string, ",", "\"", line) [initial call] */
/* p = csv_lineparse(NULL, ",", "\"", line) [subsequent calls] */
/*****************************************************************************/
char *
-csv_lineparse(char *stringstart, const char *delimited_by,
+csv_lineparse(const char *stringstart, const char *delimited_by,
const char *enclosed_in, const int line_no)
{
- char *sp;
- static char *p = NULL;
+ const char *sp;
+ static const char *p = NULL;
static char *tmp = NULL;
size_t dlen, elen;
int enclosedepth = 0;
tmp = xcalloc((p - sp) + 1, sizeof(char));
strncpy(tmp, sp, (p - sp));
+ tmp[p - sp] = '\0';
if (dfound) {
/* skip over the delimited_by */
/* function prototypes */
char *
-csv_stringtrim(char *string, const char *enclosure);
+csv_stringtrim(const char *string, const char *enclosure);
char *
-csv_lineparse(char *stringstart, const char *delimited_by, const char *enclosed_in, const int line_no);
+csv_lineparse(const char *stringstart, const char *delimited_by, const char *enclosed_in, const int line_no);
char *
-csv_stringclean(char *string, const char *chararray);
+csv_stringclean(const char *string, const char *chararray);
/* data delimited by commas, possibly enclosed in quotes. */
s = buff;
- s = csv_lineparse(s, ",", "\"", linecount);
+ s = csv_lineparse(s, ",", "\"", linecount);
+
i = 0;
while (s) {
switch (i) {
wpt_tmp->position.longitude.degrees = atof(s);
break;
case 2:
- wpt_tmp->description = xstrdup(s);
- wpt_tmp->description = csv_stringtrim(wpt_tmp->description, "");
+ wpt_tmp->description = csv_stringtrim(s, "");
break;
case 3:
- wpt_tmp->shortname = xstrdup(s);
- wpt_tmp->shortname = csv_stringtrim(wpt_tmp->shortname, "");
+ wpt_tmp->shortname = csv_stringtrim(s, "");
break;
case 4:
/* ignore. another name-type */
}
static void
-mxf_disp(waypoint * wpt)
+mxf_waypt_pr(const waypoint * wpt)
{
int icon = 47; /* default to "dot" */
const char *color_hex = "ff0000";
-
- csv_stringclean(wpt->shortname, ",\"");
- wpt->shortname = csv_stringtrim(wpt->shortname, "");
+ char *shortname = NULL;
+ char *description = NULL;
+
+ if (wpt->shortname) {
+ shortname = csv_stringclean(wpt->shortname, ",\"");
+ shortname = csv_stringtrim(shortname, "");
+ } else {
+ shortname = xstrdup("");
+ }
+
+ if (wpt->description) {
+ description = csv_stringclean(wpt->description, ",\"");
+ description = csv_stringtrim(description, "");
+ } else {
+ shortname = xstrdup("");
+ }
- csv_stringclean(wpt->description, ",\"");
- wpt->description = csv_stringtrim(wpt->description, "");
-
fprintf(file_out, "%08.5f, %08.5f, \"%s\", \"%s\", \"%s\", %s, %d\n",
wpt->position.latitude.degrees, wpt->position.longitude.degrees,
- wpt->description, wpt->shortname, wpt->description,
+ description, shortname, description,
color_hex, icon);
+
+ if (description)
+ free(description);
+ if (shortname)
+ free(shortname);
}
static void
data_write(void)
{
- waypt_disp_all(mxf_disp);
+ waypt_disp_all(mxf_waypt_pr);
}
ff_vecs_t mxf_vecs = {
break;
case 1:
/* waypoint name */
- wpt_tmp->shortname = xstrdup(s);
- wpt_tmp->shortname = csv_stringtrim(wpt_tmp->shortname, "");
+ wpt_tmp->shortname = csv_stringtrim(s, "");
break;
case 2:
/* degrees latitude */
break;
case 10:
/* Description */
- wpt_tmp->description = xstrdup(s);
- wpt_tmp->description = csv_stringtrim(wpt_tmp->description, "");
+ wpt_tmp->description = csv_stringtrim(s, "");
break;
case 11:
}
static void
-ozi_disp(waypoint * wpt)
+ozi_waypt_pr(const waypoint * wpt)
{
static int index = 0;
double alt_feet;
double ozi_time;
+ char * description;
+ char * shortname;
ozi_time = (wpt->creation_time / 86400.0) + 25569.0;
alt_feet = (wpt->position.altitude.altitude_meters * 3.2808);
- csv_stringclean(wpt->description, ",");
- csv_stringclean(wpt->shortname, ",");
+ if (wpt->description)
+ description = csv_stringclean(wpt->description, ",");
+ else
+ description = xstrdup("");
+
+ if (wpt->shortname)
+ shortname = csv_stringclean(wpt->shortname, ",");
+ else
+ shortname = xstrdup("");
index++;
fprintf(file_out, "%4d,%-14.14s,%11.6f,%11.6f,%011.5f,%3d,%2d,%2d,%10d,%10d,%-40.40s,%2d,%2d,%5d,%7.0f,%2d,%2d,%2d\n",
- index, wpt->shortname, wpt->position.latitude.degrees,
+ index, shortname, wpt->position.latitude.degrees,
wpt->position.longitude.degrees, ozi_time, 0, 1, 3, 0, 65535,
- wpt->description, 0, 0, 0, alt_feet, 6, 0, 17);
+ description, 0, 0, 0, alt_feet, 6, 0, 17);
+
+ free(description);
+ free(shortname);
}
fprintf(file_out, "Reserved 2\n");
fprintf(file_out, "Reserved 3\n");
- waypt_disp_all(ozi_disp);
+ waypt_disp_all(ozi_waypt_pr);
}
ff_vecs_t ozi_vecs = {
}
static void
-psp_disp(waypoint *wpt)
+psp_waypt_pr(const waypoint *wpt)
{
double lon, lat;
char tbuf[64];
char c;
int i;
-
- /* this output format pretty much requires a description and a shortname */
- if (!wpt->shortname) {
- if (wpt->description)
- wpt->shortname = xstrdup(wpt->description);
+ char *shortname;
+ char *description;
+
+
+ /* this output format pretty much requires a description
+ * and a shortname
+ */
+
+ if (wpt->shortname) {
+ shortname = xstrdup(wpt->shortname);
+ } else {
+ if (wpt->description)
+ shortname = xstrdup(wpt->description);
else
- wpt->shortname = xstrdup("");
- }
- if (!wpt->description) {
- wpt->description = xstrdup(wpt->shortname);
+ shortname = xstrdup("");
}
-
+
+ if (wpt->description) {
+ description = xstrdup(wpt->description);
+ } else {
+ description = xstrdup(shortname);
+ }
/* convert lat/long back to radians */
lat = (wpt->position.latitude.degrees * M_PI) / 180.0;
/* 3 unknown bytes */
fwrite(&tbuf, 1, 3, psp_file_out); /* 3 junk */
- c = strlen(wpt->shortname);
+ c = strlen(shortname);
/* 1 string size */
fwrite(&c, 1, 1, psp_file_out);
- for (i = 0 ; wpt->shortname[i] ; i++) {
- fwrite(&wpt->shortname[i], 1, 1, psp_file_out); /* char */
+ for (i = 0 ; shortname[i] ; i++) {
+ fwrite(&shortname[i], 1, 1, psp_file_out); /* char */
fwrite(&tbuf[0], 1, 1, psp_file_out); /* null */
}
- c = strlen(wpt->description);
+ c = strlen(description);
/* 1 byte string size */
fwrite(&c, 1, 1, psp_file_out);
- for (i = 0 ; wpt->description[i] ; i++) {
- fwrite(&wpt->description[i], 1, 1, psp_file_out); /* char */
+ for (i = 0 ; description[i] ; i++) {
+ fwrite(&description[i], 1, 1, psp_file_out); /* char */
fwrite(&tbuf[0], 1, 1, psp_file_out); /* null */
}
fwrite(&tbuf[i], 1, 1, psp_file_out); /* char */
fwrite(&tbuf[0], 1, 1, psp_file_out); /* null */
}
+
+ free (shortname);
+ free (description);
}
static void
fwrite(&header_bytes, 1, 32, psp_file_out);
- waypt_disp_all(psp_disp);
+ waypt_disp_all(psp_waypt_pr);
}
ff_vecs_t psp_vecs = {